package com.py.credit.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceShareLogFieldsEnum;
import com.py.credit.service.ShareLogService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;

@RestController
@RequestMapping(value = "/shareLog")
public class ShareLogController {

	private static final int ZERO = 0;

	private static final String ZERO_STR = "0";

	private final static String FRONT_TABLE_ID = "shareLog";

	@Autowired
	private ShareLogService shareLogService;

	/**
	 * 获取初始化列
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<>();
		try {
			r = new RestServiceResult<>(ResourceShareLogFieldsEnum.getOptionalColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;
	}

	/**
	 * @api {get} /shareLog/getTableList.json 获取表规则列表
	 * @apiName getTableList
	 * @apiVersion 0.0.1
	 * @apiGroup 表规则管理
	 * 
	 * @apiParam {String} condition 查询条件(可选)
	 * @apiParam {int} limit 每页展示条数 (必需)
	 * @apiParam {int} page 当前页数 (必需)
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":2,"data":[{"rule_package":"1222","rule_desc":"122222","rule_name":"1","rule_method":"123","id":22,"time":"2018-04-23","managerName":"超级管理员"},{"rule_package":"555","rule_desc":"5555","rule_name":"555","rule_method":"555","id":35,"time":"2018-04-26","managerName":"超级管理员"}]}
	 * 
	 */
	@RequestMapping(value = "/getTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page) {

		String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceShareLogFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		// condition="[{columnEN:\"batch_id\",value:\"EX-test-1-20180326-1174\",type:\"varchar\",fuzzy:fasle}]";
		RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
		try {
			List<Map<String, Object>> tableList = shareLogService.getByConditionForPage(showColumns, null, condition,
					page);
			r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}

	@RequestMapping(value = "/export")
	public RestServiceResult<List<Map<String, Object>>> exportData(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletResponse response) {
		page.setLimit(1000);
		String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceShareLogFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		List<Map<String, Object>> tableList = shareLogService.getByConditionForPage(showColumns, null, condition, page);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		try {
			ExcelUtil.downloadExcel(response, tableList, "共享日志", "共享日志", showColumns,
					ResourceShareLogFieldsEnum.getOptionalColumns());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("共享日志导出异常，请稍后再试！");
			ExceptionLogger.error("共享日志导出异常！", e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
		try {
			r = new RestServiceResult<>(ResourceShareLogFieldsEnum.getAdvancedConditionColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;

	}

	/**
	 * 手动选择展示列
	 * 
	 * @param columns
	 * @return
	 */
	@RequestMapping(value = "/addShowColunms.json")
	public RestServiceResult<String> addShowColunms(
			@RequestParam(value = "columns", required = true) String[] columns) {
		RestServiceResult<String> r = new RestServiceResult<String>();
		try {
			MapCache.set(1000 + "_" + FRONT_TABLE_ID, columns);
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;
	}

}
